package com.scalar.chiptrack.adaptor.umc;

import java.net.URL;

import java.util.Calendar;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedInputStream;

import HTTPClient.HTTPConnection;
import HTTPClient.HTTPResponse;
import HTTPClient.NVPair;
import HTTPClient.CookieModule;

import Acme.HtmlScanner;
import com.scalar.ScalarException;
import com.scalar.chiptrack.users.VendorAdaptor;
import com.scalar.chiptrack.users.dao.AdaptorSetUpDAO;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;


public class UMCAdaptor extends JobHandler implements AdaptorConstants
{
    private static final String PROTOCOL_SCHEME_HTTPS   = "https";
    private static final String HOST        = "my.umc.com";
    //private static final int PORT         = 80;
    private static final int SSL_PORT       = 443;
    public static final String ADAPTOR_NAME = "UMC_FAB";

    private static final String URI_LOGIN_FORM_ACTION   = "/secure/cishttpslogin.fcc";
    private static final String URL_LOGIN_TARGET        = "https://my.umc.com/cisweb/";
    private static final String URI_WIP_REPORT          = "/cisweb/logistics/wip_qry/wip_dynamic_gen.asp";
    private static final String URI_DYN_SHIPPING_INFO   = "/cisweb/logistics/sales_qry/dynamic_ship_submit.asp";
    private static final String URI_LOT_BIN_SUMMARY     = "/cisweb/quality/static_rpt/ReportFunction.asp?FirstVisit=1&ReportName=CP_BIN";
    public static final String REDIRECT_SIGNIN          = "/auth_reject.htm";
                                                       
    public static final String FILE_NAME_STANDARD_WIP_SUMMARY   = "Dynamic_WIP_Report";
    public static final String FILE_NAME_SHIPPING_INFO          = "Dynamic_Shipping_Report";
    public static final String FILE_NAME_SORT_LOT_BIN_SUMMARY   = "CP_Bin_Summary";


    // CLASS VARIABLES
    private static URL s_dummyUrl       = null;
    private UMCHtmlObserver m_observer  = null;
    private Logger m_adaptorLogger      = null;
    private String m_dataOutputDir      = null;
    private String m_username           = null;
    private String m_password           = null;
    private int    m_adaptor_id         = -1;

    public UMCAdaptor() throws Exception
    {

        if ( s_dummyUrl == null )
        {
            try
            {
                s_dummyUrl = new URL( "https://" + HOST + "/" );
            }
            catch ( Exception ignore )
            {
                ignore.printStackTrace();
                throw ignore;
            }
        }

        try
        {
            VendorAdaptor vendorAdaptor = AdaptorSetUpDAO.getVendorAdaptorDetailsByVendorNameAndLocation( VENDOR_NAME_UMC, LOCATION_TYPE_FAB );

            m_username = vendorAdaptor.getUserId();
            m_password = vendorAdaptor.getPassWord();
            m_dataOutputDir = vendorAdaptor.getOutPutDirectory();

            if ( m_dataOutputDir != null )
            {
                if ( m_dataOutputDir.endsWith( "/" ) || m_dataOutputDir.endsWith( "\\" ) )
                {
                    m_dataOutputDir = m_dataOutputDir + m_username;
                }
                else
                {
                    m_dataOutputDir = m_dataOutputDir + "/" + m_username;
                }

                AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );

                m_adaptorLogger = AdaptorUtils.getAdaptorLogger( m_dataOutputDir , VENDOR_NAME_UMC);
                AdaptorUtils.setLogger( m_adaptorLogger );
            }
        }
        catch ( ScalarException se )
        {
            log( "ERROR!! ERROR!! ERROR!! Exception while loading UMC Adaptor Username, Password details from database, Exception: " + se );
            throw se;
        }
        m_observer = new UMCHtmlObserver();
    }


    public static void main( String[] args ) throws Exception
    {
        new UMCAdaptor().execute();
    }

    public void execute() throws Exception
    {
        try
        {
            m_adaptor_id = AdaptorRunInfo.saveAdaptorLastUpdatedStatus( CONSTANT_ADAPTOR, ADAPTOR_NAME, CONSTANT_SUCCESS, -1);

            // Step 1
            // prints the version of Adaptor
            AdaptorUtils.printHeader(VENDOR_NAME_UMC, CONSTANT_ADAPTOR );

            // Step 2
            // removes the old files out side done directory
            AdaptorUtils.removeOldFiles(m_dataOutputDir, m_adaptorLogger);

            // Login to UMC Site

            HTTPConnection m_connection = login( HOST, SSL_PORT, m_username, m_password );

            if ( m_connection != null )
            {
                // Retrieve and Save WIP Report

                HTTPResponse rspWip = requestWIPInfo( m_connection );
                if ( rspWip != null )
                {
                    AdaptorUtils.saveAsFile( getReader( m_connection, rspWip ), m_dataOutputDir, FILE_NAME_STANDARD_WIP_SUMMARY , FILE_EXTN_CSV );
                    if ( m_adaptor_id > 0 )
                    {
                        AdaptorRunInfo.saveAdaptorFileInfo( m_adaptor_id , FILE_NAME_STANDARD_WIP_SUMMARY, FILE_EXTN_CSV, StringUtils.formatDateandTime(DateUtil.getFormattedTimeStamp() )) ;
                    }
                }

                // Retrieve and Save Shipping information
                HTTPResponse rsp = requestShippingInfo( m_connection );
                if ( rsp != null )
                {
                    AdaptorUtils.saveAsFile( getReader( m_connection, rsp ), m_dataOutputDir, FILE_NAME_SHIPPING_INFO, FILE_EXTN_CSV );
                    if ( m_adaptor_id > 0 )
                    {
                        AdaptorRunInfo.saveAdaptorFileInfo( m_adaptor_id , FILE_NAME_SHIPPING_INFO, FILE_EXTN_CSV, StringUtils.formatDateandTime(DateUtil.getFormattedTimeStamp() )) ;
                    }
                }

                //TODOWNLOAD SAVE the BINNING FILES
                downloadLotBinSummary( m_connection );
            }
        }
        catch ( Exception e )
        {
            AdaptorUtils.removeOldFiles( m_dataOutputDir, m_adaptorLogger);
            AdaptorRunInfo.updatedAdaptorStatus( CONSTANT_FAILURE, m_adaptor_id );
            System.out.println("Exception while trying to connect and read data from "+HOST +" site: " + e);
            e.printStackTrace();
            throw e;
        }
    }


    /**
     *
     * @param host    hostName
     * @param port     portNumber
     * @param userName  userLoign
     * @param password userpassword
     * @return     connection
     * @throws Exception   anyException
     */
    private HTTPConnection login( String host, int port, String userName, String password ) throws Exception
    {
        byte[] data = new byte[1024];

        NVPair defaultHeaders[] = new NVPair[9];
        defaultHeaders[0] = new NVPair( "Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, */*" );
        defaultHeaders[1] = new NVPair( "Referer", "https://" + host );
        defaultHeaders[2] = new NVPair( "Accept-Language", "en-us" );
        defaultHeaders[3] = new NVPair( "Content-Type", "application/x-www-form-urlencoded" );
        defaultHeaders[4] = new NVPair( "Proxy-Connection", "Keep-Alive" );
        defaultHeaders[5] = new NVPair( "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" );
        defaultHeaders[6] = new NVPair( "Host", host );
        defaultHeaders[8] = new NVPair( "Pragma", "no-cache" );

        HTTPConnection m_connection = new HTTPConnection(PROTOCOL_SCHEME_HTTPS, host, port );

        m_connection.setDefaultHeaders( defaultHeaders );

        // Accept all cookies
        CookieModule.setCookiePolicyHandler( null );
        m_connection.addDefaultModule( CookieModule.class, -1 );

        log( "\nTrying to log in... " );
        System.out.println( "\nTrying to log in... " );

        HTTPResponse rsp = m_connection.Get( "/" );

        /*while ( rsp.getInputStream().read( data ) != -1 )
        {
            System.out.println(new String (data));
        }*/
        BufferedReader reader = new BufferedReader( new InputStreamReader( rsp.getInputStream() ) );
        
        String line = null;
        String dynamicToken = null;
        while ( ( line = reader.readLine() ) != null )
        {
            if ( line.indexOf( "name=smagentname value" ) != -1 )
            {
                int index1 = line.indexOf( "\"" );
                int index2 = line.indexOf( "\"", index1 + 1 );

                dynamicToken = line.substring( index1 + 1, index2 );
            }
        }

        NVPair[] formData = new NVPair[11];
        formData[0] = new NVPair( "TYPE", "33554433" );
        formData[1] = new NVPair( "REALMOID", "06-b91c997d-0565-4423-989b-954dc5db9cee" );
        formData[2] = new NVPair( "GUID", "" );
        formData[3] = new NVPair( "SMENC", "ISO-8859-1" );
        formData[4] = new NVPair( "SMLOCALE", "US-EN" );
        formData[5] = new NVPair( "target", URL_LOGIN_TARGET );
        formData[6] = new NVPair( "smauthreason", "0" );
        formData[7] = new NVPair( "smagentname", dynamicToken );
        formData[8] = new NVPair( "postpreservationdata", "" );
        formData[9] = new NVPair( "USERNAME", userName );
        formData[10] = new NVPair( "PASSWORD", password );

        rsp = m_connection.Post( URI_LOGIN_FORM_ACTION, formData );

        if( rsp.getEffectiveURI().toString().endsWith( REDIRECT_SIGNIN ) ||  rsp.getEffectiveURI().toString().indexOf("menu=expired_pwd") != -1 )
        {
            throw new ScalarException("********ERROR!! ERROR!! ERROR!! Password was Expired,, Pls Reset**********\n\n ");
        }
        /*while ( rsp.getInputStream().read( data ) != -1 )
        {
            //log (new String (data));
        }*/

        log( "successfully logged in\n" );
        System.out.println( "successfully logged in\n" );

        return  m_connection;
    }

    private HTTPResponse requestShippingInfo( HTTPConnection con ) throws Exception
    {                   
        log( "Submitting HTTP Request for Shipping Information... " );
        System.out.println( "Submitting HTTP Request for Shipping Information... " );

        NVPair[] formData = new NVPair[166];

        formData[0] = new NVPair( "fieldtype1", "must" );
        formData[1] = new NVPair( "fieldname1", "SHP_PRD_NO" );
        formData[2] = new NVPair( "fieldtype2", "must" );
        formData[3] = new NVPair( "fieldname2", "LOT_NO" );
        formData[4] = new NVPair( "fieldtype3", "must" );
        formData[5] = new NVPair( "fieldname3", "SHIP_W_QTY" );
        formData[6] = new NVPair( "fieldtype4", "must" );
        formData[7] = new NVPair( "fieldname4", "INV_DATE" );
        formData[8] = new NVPair( "field5", "on" );
        formData[9] = new NVPair( "fieldname5", "WAFER_NO" );
        formData[10] = new NVPair( "fieldtype5", "optional" );
        formData[11] = new NVPair( "field6", "on" );
        formData[12] = new NVPair( "fieldname6", "PLANT" );
        formData[13] = new NVPair( "fieldtype6", "optional" );
        formData[14] = new NVPair( "field7", "on" );
        formData[15] = new NVPair( "fieldname7", "REAL_PO" );
        formData[16] = new NVPair( "fieldtype7", "optional" );
        formData[17] = new NVPair( "field8", "on" );
        formData[18] = new NVPair( "fieldname8", "PO_NO" );
        formData[19] = new NVPair( "fieldtype8", "optional" );
        formData[20] = new NVPair( "field9", "on" );
        formData[21] = new NVPair( "fieldname9", "SO_NO" );
        formData[22] = new NVPair( "fieldtype9", "optional" );
        formData[23] = new NVPair( "field10", "on" );
        formData[24] = new NVPair( "fieldname10", "LINE_NO" );
        formData[25] = new NVPair( "fieldtype10", "optional" );
        formData[26] = new NVPair( "field11", "on" );
        formData[27] = new NVPair( "fieldname11", "LINE_VER" );
        formData[28] = new NVPair( "fieldtype11", "optional" );
        formData[29] = new NVPair( "field12", "on" );
        formData[30] = new NVPair( "fieldname12", "CT_PO_LINE" );
        formData[31] = new NVPair( "fieldtype12", "optional" );
        formData[32] = new NVPair( "field13", "on" );
        formData[33] = new NVPair( "fieldname13", "RELEASE_PO" );
        formData[34] = new NVPair( "fieldtype13", "optional" );
        formData[35] = new NVPair( "field14", "on" );
        formData[36] = new NVPair( "fieldname14", "CUST_PART" );
        formData[37] = new NVPair( "fieldtype14", "optional" );
        formData[38] = new NVPair( "field15", "on" );
        formData[39] = new NVPair( "fieldname15", "CTM_DEVICE" );
        formData[40] = new NVPair( "fieldtype15", "optional" );
        formData[41] = new NVPair( "field16", "on" );
        formData[42] = new NVPair( "fieldname16", "PRD_NO" );
        formData[43] = new NVPair( "fieldtype16", "optional" );
        formData[44] = new NVPair( "field17", "on" );
        formData[45] = new NVPair( "fieldname17", "MTH_NO" );
        formData[46] = new NVPair( "fieldtype17", "optional" );
        formData[47] = new NVPair( "field18", "on" );
        formData[48] = new NVPair( "fieldname18", "MTH_FORM" );
        formData[49] = new NVPair( "fieldtype18", "optional" );
        formData[50] = new NVPair( "field19", "on" );
        formData[51] = new NVPair( "fieldname19", "PACK_NO" );
        formData[52] = new NVPair( "fieldtype19", "optional" );
        formData[53] = new NVPair( "field20", "on" );
        formData[54] = new NVPair( "fieldname20", "PACK_DATE" );
        formData[55] = new NVPair( "fieldtype20", "optional" );
        formData[56] = new NVPair( "field21", "on" );
        formData[57] = new NVPair( "fieldname21", "INV_NO" );
        formData[58] = new NVPair( "fieldtype21", "optional" );
        formData[59] = new NVPair( "field22", "on" );
        formData[60] = new NVPair( "fieldname22", "RECEIPT_NO" );
        formData[61] = new NVPair( "fieldtype22", "optional" );
        formData[62] = new NVPair( "field23", "on" );
        formData[63] = new NVPair( "fieldname23", "INV_TYPE" );
        formData[64] = new NVPair( "fieldtype23", "optional" );
        formData[65] = new NVPair( "field24", "on" );
        formData[66] = new NVPair( "fieldname24", "SHPTO_ID" );
        formData[67] = new NVPair( "fieldtype24", "optional" );
        formData[68] = new NVPair( "field25", "on" );
        formData[69] = new NVPair( "fieldname25", "SHIP_NO" );
        formData[70] = new NVPair( "fieldtype25", "optional" );
        formData[71] = new NVPair( "field26", "on" );
        formData[72] = new NVPair( "fieldname26", "FAB_LOT_NO" );
        formData[73] = new NVPair( "fieldtype26", "optional" );
        formData[74] = new NVPair( "field27", "on" );
        formData[75] = new NVPair( "fieldname27", "LOT_TYPE" );
        formData[76] = new NVPair( "fieldtype27", "optional" );
        formData[77] = new NVPair( "field28", "on" );
        formData[78] = new NVPair( "fieldname28", "SHIP_D_QTY" );
        formData[79] = new NVPair( "fieldtype28", "optional" );
        formData[80] = new NVPair( "field29", "on" );
        formData[81] = new NVPair( "fieldname29", "WS_DATE" );
        formData[82] = new NVPair( "fieldtype29", "optional" );
        formData[83] = new NVPair( "field30", "on" );
        formData[84] = new NVPair( "fieldname30", "YIELD" );
        formData[85] = new NVPair( "fieldtype30", "optional" );
        formData[86] = new NVPair( "field31", "on" );
        formData[87] = new NVPair( "fieldname31", "MAWB_NO" );
        formData[88] = new NVPair( "fieldtype31", "optional" );
        formData[89] = new NVPair( "field32", "on" );
        formData[90] = new NVPair( "fieldname32", "HAWB_NO" );
        formData[91] = new NVPair( "fieldtype32", "optional" );
        formData[92] = new NVPair( "field33", "on" );
        formData[93] = new NVPair( "fieldname33", "FLT_INFO_1" );
        formData[94] = new NVPair( "fieldtype33", "optional" );
        formData[95] = new NVPair( "field34", "on" );
        formData[96] = new NVPair( "fieldname34", "FLT_INFO_2" );
        formData[97] = new NVPair( "fieldtype34", "optional" );
        formData[98] = new NVPair( "field35", "on" );
        formData[99] = new NVPair( "fieldname35", "FLT_INFO_3" );
        formData[100] = new NVPair( "fieldtype35", "optional" );
        formData[101] = new NVPair( "field36", "on" );
        formData[102] = new NVPair( "fieldname36", "PART_DIV" );
        formData[103] = new NVPair( "fieldtype36", "optional" );
        formData[104] = new NVPair( "field37", "on" );
        formData[105] = new NVPair( "fieldname37", "C_P_UNIT" );
        formData[106] = new NVPair( "fieldtype37", "optional" );
        formData[107] = new NVPair( "field38", "on" );
        formData[108] = new NVPair( "fieldname38", "ORDER_TYPE_BRF" );
        formData[109] = new NVPair( "fieldtype38", "optional" );
        formData[110] = new NVPair( "field39", "on" );
        formData[111] = new NVPair( "fieldname39", "PO_DATE" );
        formData[112] = new NVPair( "fieldtype39", "optional" );
        formData[113] = new NVPair( "field40", "on" );
        formData[114] = new NVPair( "fieldname40", "PRD_LAYER" );
        formData[115] = new NVPair( "fieldtype40", "optional" );
        formData[116] = new NVPair( "field41", "on" );
        formData[117] = new NVPair( "fieldname41", "PROCESSGEN" );
        formData[118] = new NVPair( "fieldtype41", "optional" );
        formData[119] = new NVPair( "field42", "on" );
        formData[120] = new NVPair( "fieldname42", "PROCESSFAMILY" );
        formData[121] = new NVPair( "fieldtype42", "optional" );
        formData[122] = new NVPair( "field43", "on" );
        formData[123] = new NVPair( "fieldname43", "CT_LOT_NO" );
        formData[124] = new NVPair( "fieldtype43", "optional" );
        formData[125] = new NVPair( "field44", "on" );
        formData[126] = new NVPair( "fieldname44", "UMC_WAREHOUSE" );
        formData[127] = new NVPair( "fieldtype44", "optional" );
        formData[128] = new NVPair( "field45", "on" );
        formData[129] = new NVPair( "fieldname45", "CUSTOM_CLEARANCE" );
        formData[130] = new NVPair( "fieldtype45", "optional" );
        formData[131] = new NVPair( "field46", "on" );
        formData[132] = new NVPair( "fieldname46", "FORWARDER_HANDS" );
        formData[133] = new NVPair( "fieldtype46", "optional" );
        formData[134] = new NVPair( "field47", "on" );
        formData[135] = new NVPair( "fieldname47", "ETD" );
        formData[136] = new NVPair( "fieldtype47", "optional" );
        formData[137] = new NVPair( "field48", "on" );
        formData[138] = new NVPair( "fieldname48", "ETA" );
        formData[139] = new NVPair( "fieldtype48", "optional" );
        formData[140] = new NVPair( "field49", "on" );
        formData[141] = new NVPair( "fieldname49", "GSM_PACK_NO" );
        formData[142] = new NVPair( "fieldtype49", "optional" );
        formData[143] = new NVPair( "field50", "on" );
        formData[144] = new NVPair( "fieldname50", "WO_DATE" );
        formData[145] = new NVPair( "fieldtype50", "optional" );
        formData[146] = new NVPair( "field51", "on" );
        formData[147] = new NVPair( "fieldname51", "PROCESS" );
        formData[148] = new NVPair( "fieldtype51", "optional" );
        formData[149] = new NVPair( "field52", "on" );
        formData[150] = new NVPair( "fieldname52", "PROCESSVERSION" );
        formData[151] = new NVPair( "fieldtype52", "optional" );
        formData[152] = new NVPair( "plant", "PLANT" );
        formData[153] = new NVPair( "plant_value", "*" );
        formData[154] = new NVPair( "ship_by", "SHP_PRD_NO" );
        formData[155] = new NVPair( "SHP_PRD_NO", "*" );
        formData[156] = new NVPair( "GroupBy", "CUST_PART" );
        formData[157] = new NVPair( "OrderBy_1", "REAL_PO" );
        formData[158] = new NVPair( "Sort_1", "Asc" );
        formData[159] = new NVPair( "OrderBy_2", "REAL_PO" );
        formData[160] = new NVPair( "Sort_2", "Asc" );
        formData[161] = new NVPair( "OrderBy_3", "REAL_PO" );
        formData[162] = new NVPair( "Sort_3", "Asc" );
        formData[163] = new NVPair( "FieldCount", "52" );

        System.out.println( "\tRequesting Shipping information from " + DateUtil.getPreviousDate( 10, "yyyy/MM/dd") +
                " to " + DateUtil.getCurrentDate("yyyy/MM/dd") );
        formData[164] = new NVPair( "begin_date", DateUtil.getPreviousDate( 10, "yyyy/MM/dd") );
        formData[165] = new NVPair( "end_date", DateUtil.getCurrentDate("yyyy/MM/dd") );

        HTTPResponse rsp = con.Post( URI_DYN_SHIPPING_INFO, formData );
        log( "done\n" );
        System.out.println( "done\n" );

        return rsp;
    }

    private HTTPResponse requestWIPInfo( HTTPConnection con ) throws Exception
    {
        log( "Submitting HTTP Request for Wip Information... " );
        System.out.println( "Submitting HTTP Request for wip Information... " );

        NVPair[] formData = new NVPair[86];

        formData[0] = new NVPair( "field1", "on" );
        formData[1] = new NVPair( "fieldname1", "WAFER_NO" );

        formData[2] = new NVPair( "field2", "on" );
        formData[3] = new NVPair( "fieldname2", "STAGE" );

        formData[4] = new NVPair( "field3", "on" );
        formData[5] = new NVPair( "fieldname3", "PO" );

        formData[6] = new NVPair( "fieldname4", "SITE_LOT" );

        formData[7] = new NVPair( "field5", "on" );
        formData[8] = new NVPair( "fieldname5", "PROCESS" );

        formData[9] = new NVPair( "field6", "on" );
        formData[10] = new NVPair( "fieldname6", "PROCESSVERSION" );

        formData[11] = new NVPair( "field7", "on" );
        formData[12] = new NVPair( "fieldname7", "SHP_PRD_NO" );

        formData[13] = new NVPair( "field8", "on" );
        formData[14] = new NVPair( "fieldname8", "CUST_PART" );

        formData[15] = new NVPair( "field9", "on" );
        formData[16] = new NVPair( "fieldname9", "CTM_DEVICE" );

        formData[17] = new NVPair( "field10", "on" );
        formData[18] = new NVPair( "fieldname10", "PRD_NO" );

        formData[19] = new NVPair( "field11", "on" );
        formData[20] = new NVPair( "fieldname11", "ROUTESEQUENCE" );

        formData[21] = new NVPair( "field12", "on" );
        formData[22] = new NVPair( "fieldname12", "ROUTEPOSITION" );

        formData[23] = new NVPair( "field13", "on" );
        formData[24] = new NVPair( "fieldname13", "ROUTEDESC" );

        formData[25] = new NVPair( "field14", "on" );
        formData[26] = new NVPair( "fieldname14", "WS_DATE" );

        formData[27] = new NVPair( "field15", "on" );
        formData[28] = new NVPair( "fieldname15", "WS_QTY" );

        formData[29] = new NVPair( "field16", "on" );
        formData[30] = new NVPair( "fieldname16", "GROSS_DIE" );

        formData[31] = new NVPair( "field17", "on" );
        formData[32] = new NVPair( "fieldname17", "IN_TIME" );


        formData[33] = new NVPair( "field18", "on" );
        formData[34] = new NVPair( "fieldname18", "WO_FCST_DATE" );

        formData[35] = new NVPair( "field19", "on" );
        formData[36] = new NVPair( "fieldname19", "WO_DATE" );

        formData[37] = new NVPair( "field20", "on" );
        formData[38] = new NVPair( "fieldname20", "SHIP_FCST_DATE" );

        formData[39] = new NVPair( "field21", "on" );
        formData[40] = new NVPair( "fieldname21", "SHIP_CONF_DATE" );

        formData[41] = new NVPair( "field22", "on" );
        formData[42] = new NVPair( "fieldname22", "PRIORITY" );

        formData[43] = new NVPair( "field23", "on" );
        formData[44] = new NVPair( "fieldname23", "LOTSTATUS" );

        formData[45] = new NVPair( "field24", "on" );
        formData[46] = new NVPair( "fieldname24", "RETICLEVERSION" );

        formData[47] = new NVPair( "field25", "on" );
        formData[48] = new NVPair( "fieldname25", "CUSTOMERLOT" );

        formData[49] = new NVPair( "field26", "on" );
        formData[50] = new NVPair( "fieldname26", "PROCESSGEN" );

        formData[51] = new NVPair( "field27", "on" );
        formData[52] = new NVPair( "fieldname27", "PROCESSFAMILY" );

        formData[53] = new NVPair( "fieldname28", "SUBCONTRACT`" );

        formData[54] = new NVPair( "field29", "on" );
        formData[55] = new NVPair( "fieldname29", "LOTTYPE" );

        formData[56] = new NVPair( "field30", "on" );
        formData[57] = new NVPair( "fieldname30", "STAY_DAY" );

        formData[58] = new NVPair( "field31", "on" );
        formData[59] = new NVPair( "fieldname31", "BACK_DAY" );

        formData[60] = new NVPair( "field32", "on" );
        formData[61] = new NVPair( "fieldname32", "SHIPTO_ID" );

        formData[62] = new NVPair( "field33", "on" );
        formData[63] = new NVPair( "fieldname33", "LAYER" );

        formData[64] = new NVPair( "field34", "on" );
        formData[65] = new NVPair( "fieldname34", "LAYER_POSITION" );

        formData[66] = new NVPair( "fieldname35", "MTH_NO" );

        formData[67] = new NVPair( "fieldname36", "RELEASE_PO" );

        formData[68] = new NVPair( "field_Count", "36" );

        formData[69] = new NVPair( "rpttype", "0" );

        formData[70] = new NVPair( "FabName", "%" );
        formData[71] = new NVPair( "Location", "ALL" );

        formData[72] = new NVPair( "shp_prd_type", "0" );
        formData[73] = new NVPair( "shp_prd_no", "%" );
        formData[74] = new NVPair( "SHP_PRD_NO_Con", "ALL" );

        formData[75] = new NVPair( "Product_Type", "0" );
        formData[76] = new NVPair( "prd_no", "%" );
        formData[77] = new NVPair( "prd_no_Con", "ALL" );

        formData[78] = new NVPair( "groupby", "None" );
        formData[79] = new NVPair( "sortby1", "SHP_PRD_NO" );
        formData[80] = new NVPair( "Sort_1", "Desc" );
        formData[81] = new NVPair( "sortby2", "SHP_PRD_NO" );
        formData[82] = new NVPair( "Sort_2", "Desc" );
        formData[83] = new NVPair( "sortby3", "SHP_PRD_NO" );
        formData[84] = new NVPair( "Sort_3", "Desc" );
        formData[85] = new NVPair( "b1", "Query" );

        HTTPResponse rsp = con.Post( URI_WIP_REPORT , formData );
        return rsp;
    }
    private void downloadLotBinSummary(HTTPConnection con) throws Exception
    {

        // STEP 1
        // SUBMIT REQUEST
        log( "Submitting HTTP Request for Lot Bin Summary List..." );
        HTTPResponse rsp = con.Get( URI_LOT_BIN_SUMMARY );
              

        UMCHtmlParser extractor = new UMCHtmlParser();
        ArrayList linksList = extractor.parse( new BufferedReader( new InputStreamReader( rsp.getInputStream() ) ) );

        if ( linksList != null && linksList.size() > 0 )
        {
            Iterator keys = linksList.iterator();
            String fileName = null;
            String downloadLink = null;
            BufferedInputStream bistream = null;

            while ( keys.hasNext() )
            {
                downloadLink = ( String ) keys.next();

                fileName =  getLotBinSummaryFileName( downloadLink );

                log( "Downloading data for " + fileName + "... " );
                System.out.println( "Downloading data for " + fileName + "... " );

                rsp = con.Get( downloadLink );
                bistream =   new BufferedInputStream( rsp.getInputStream() );

                AdaptorUtils.saveAsBinaryFile( bistream, m_dataOutputDir, FILE_NAME_SORT_LOT_BIN_SUMMARY + "_" + fileName, FILE_EXTN_XLS, true);
                if ( m_adaptor_id > 0 )
                {
                    AdaptorRunInfo.saveAdaptorFileInfo( m_adaptor_id , fileName, FILE_EXTN_XLS, StringUtils.formatDateandTime(DateUtil.getFormattedTimeStamp() )) ;
                }
            }

        }
    }

    private String getLotBinSummaryFileName( String downLoadLink )
    {

        String fileName = "";
        StringTokenizer st = new StringTokenizer( downLoadLink, "?&" );
        st.nextToken();
        String pfile = st.nextToken();

        if ( st != null )
        {
            st = null;
        }

        st = new StringTokenizer( pfile, "&" );
        pfile = st.nextToken();
        st = null;
        st = new StringTokenizer( pfile, "=" );
        st.nextToken();

        fileName = st.nextToken();

        //To meet with AdaptorUtils.saveAsFile() method
        fileName = fileName.replaceAll( ".XLS", "" );

        return fileName;
    }
    /**
     * Scans for Download link and Sends HTTP Request to the download link and returns the Reader
     *
     * @param rsp Response object which is scanned for the download link
     * @return BufferedReader To read the data
     *
     * @throws Exception If there are any problems while sending HTTP request
     */
    private BufferedReader getReader( HTTPConnection con, HTTPResponse rsp ) throws Exception
    {

        byte[] data = new byte[1024];
        log( "Scanning for Download link .. " );
        BufferedReader reader = null;
        m_observer.setDownloadURL(null);
        HtmlScanner htmlScanner = new HtmlScanner( rsp.getInputStream(), s_dummyUrl, m_observer );
        while ( htmlScanner.read( data ) != -1 )
        {
            //log (new String(data));
        }
        log( "done\n" );

        String downloadLink = m_observer.getDownloadURL();
        log( "Downloading data from " + downloadLink + "... " );
        if ( downloadLink == null )
        {
            rsp = null;
            return reader;
        }
        rsp = con.Get( downloadLink );
        reader =   new BufferedReader( new InputStreamReader( rsp.getInputStream() ) );

        return reader;
    }

    private void log( String message )
    {

        if ( message == null ) return;

        if ( m_adaptorLogger != null )
        {
            synchronized ( m_adaptorLogger )
            {
                m_adaptorLogger.logDebug( message + LINE_SEPARATOR );
            }
        }
        else
        {
            System.out.println( message );
        }
    }
}